package com.zingrow.web.report.utils;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {
    private static final int FIRST_DAY = Calendar.SUNDAY;
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) {
        Date createTime = new Date();
        Calendar calendar = DateUtils.getZero(createTime);
        calendar.add(Calendar.DATE, -7);
        setToFirstDay(calendar);
    }

    /**
     * 根据传入的日期获取所在月份所有日期
     */
    @SuppressWarnings("deprecation")
    public static List<Date> getAllMonthDays(Date d) {
        List<Date> lst = new ArrayList<>();
        Date date = getMonthStart(d);
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        Date monthEnd = getMonthEnd(d);
        while (!date.after(monthEnd)) {
            lst.add(date);
            date = getNext(date);
        }
        return lst;
    }

    /**
     * 根据日期来获取其所在周的每一天
     */
    public static List<Date> getAllWeekDays(Date d) {
        Calendar c = Calendar.getInstance();
        List<Date> lst = new ArrayList<>();
        c.setTime(d);
        setToFirstDay(c);
        setZero(c);
        for (int i = 0; i < 7; i++) {
            lst.add(c.getTime());
            c.add(Calendar.DATE, 1);
        }
        return lst;
    }

    /**
     * 根据传入日期来获取一个月的开始时间
     */
    public static String getMonthStartStr(Date d) {
        return sdf.format(getMonthStart(d));
    }

    /**
     * 根据传入时间获取一个月月末时间
     */
    public static String getMonthEndStr(Date d) {

        return sdf.format(getMonthEnd(d));
    }

    /**
     * 将字符串转化为日期
     */
    public static Date paraseStringToDate(String timestr) {
        return paraseStringToDate(timestr, "yyyy-MM-dd");
    }

    /**
     * 将字符串转化为日期
     */
    public static Date paraseStringToDate(String timestr, String format) {
        Date date = null;
        Format f = new SimpleDateFormat(format);
        try {
            date = (Date) f.parseObject(timestr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 根据日期来获取一周的第一天
     */
    public static String getWeekStartDay(Date d) {
        Calendar c = Calendar.getInstance();
        List<String> lst = new ArrayList<>();
        c.setTime(d);
        setToFirstDay(c);
        for (int i = 0; i < 7; i++) {
            String day = printDate(c);
            lst.add(day);
            c.add(Calendar.DATE, 1);
        }
        return lst.get(0);
    }

    /**
     * 根据日期来获取一周的最后一天
     */
    public static String getWeekEndtDay(Date d) {
        Calendar c = Calendar.getInstance();
        List<String> lst = new ArrayList<>();
        c.setTime(d);
        setToFirstDay(c);
        for (int i = 0; i < 7; i++) {
            String day = printDate(c);
            lst.add(day);
            c.add(Calendar.DATE, 1);
        }
        return lst.get(6);
    }

    /**
     * 获取月末日期
     */
    public String getMonthEnd() {
        Date d = new Date();
        return sdf.format(getMonthEnd(d));
    }

    public static Date getMonthStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int index = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.DATE, (1 - index));
        return calendar.getTime();
    }

    public static Date getMonthEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        int index = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.DATE, (-index));
        return calendar.getTime();
    }

    private static Date getNext(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        return calendar.getTime();
    }

    public static void setToFirstDay(Calendar calendar) {
        while (calendar.get(Calendar.DAY_OF_WEEK) != FIRST_DAY) {
            calendar.add(Calendar.DATE, -1);
        }
    }

    public static String printDate(Calendar calendar) {
        return sdf.format(calendar.getTime());
    }

    public static String printDate(Date date) {
        return sdf.format(date);
    }

    public static String printDate(Date date, String format) {
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 设置calendar的时分秒为0-即到凌晨0点
     * 
     * @param calendar
     */
    public static void setZero(Calendar c) {
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
    }

    /**
     * 获取当天凌晨Calendar对象
     * 
     * @return
     */
    public static Calendar getZero(Date date) {
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        setZero(calendar);
        return calendar;
    }
}
